package com.huatai.model.service;

import cn.hutool.core.collection.CollectionUtil;
import com.huatai.model.entity.DictionaryEntity;
import com.huatai.model.mapper.DictionaryMapper;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author Lion
 * @date 2024/4/7  13:41
 */
@Service
public class DictionaryService extends BaseService<DictionaryMapper, DictionaryEntity>{

	/**
	 * 构造数据的扩展信息,空方法，继承后实现
	 *
	 * @param datas
	 * @return
	 */
	public List<DictionaryEntity> buildExInfo(List<DictionaryEntity> datas) {

		List<DictionaryEntity> list = CollectionUtil.newArrayList();
		for(DictionaryEntity treeNode : datas){
			if("0".equals(treeNode.getPid())){
				list.add(treeNode);
				innerBuild(datas,treeNode);
			}
		}
		return list;
	}

	private void innerBuild(List<DictionaryEntity> treeNodes, DictionaryEntity parentNode){
		for(DictionaryEntity childNode : treeNodes){
			if(parentNode.getId().toString().equals(childNode.getPid())){
				List<DictionaryEntity> children = parentNode.getChilds();
				if(children == null){
					children = CollectionUtil.newArrayList();
					parentNode.setChilds(children);
				}
				children.add(childNode);
				childNode.setPid(parentNode.getId().toString());
				innerBuild(treeNodes,childNode);
			}
		}
	}
}
